package com.sxsihe.heartmate;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.sxsihe.heartmate.util.SharePreferenceUtil;
import com.sxsihe.heartmate.view.RoundImageView;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by mac on 16/9/20.
 */
public class ColorChooseActivity extends Activity implements View.OnClickListener {
    RoundImageView roundImage_two_border1,roundImage_two_border2,roundImage_two_border3,roundImage_two_border4,roundImage_two_border5,roundImage_two_border6,roundImage_two_border7,roundImage_two_border8,roundImage_two_border9;
    ImageView back_icon;
    Button complete;
    ArrayList<RoundImageView> colorArray = new ArrayList<>();
    int color;
    String item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.color_choose);
        color = getIntent().getIntExtra("color",0);
        item = getIntent().getStringExtra("item");
        roundImage_two_border1 = (RoundImageView)findViewById(R.id.roundImage_two_border1);
        roundImage_two_border1.setOnClickListener(this);
        roundImage_two_border2 = (RoundImageView)findViewById(R.id.roundImage_two_border2);
        roundImage_two_border2.setOnClickListener(this);
        roundImage_two_border3 = (RoundImageView)findViewById(R.id.roundImage_two_border3);
        roundImage_two_border3.setOnClickListener(this);
        roundImage_two_border4 = (RoundImageView)findViewById(R.id.roundImage_two_border4);
        roundImage_two_border4.setOnClickListener(this);
        roundImage_two_border5 = (RoundImageView)findViewById(R.id.roundImage_two_border5);
        roundImage_two_border5.setOnClickListener(this);
        roundImage_two_border6 = (RoundImageView)findViewById(R.id.roundImage_two_border6);
        roundImage_two_border6.setOnClickListener(this);
        roundImage_two_border7 = (RoundImageView)findViewById(R.id.roundImage_two_border7);
        roundImage_two_border7.setOnClickListener(this);
        roundImage_two_border8 = (RoundImageView)findViewById(R.id.roundImage_two_border8);
        roundImage_two_border8.setOnClickListener(this);
        roundImage_two_border9 = (RoundImageView)findViewById(R.id.roundImage_two_border9);
        roundImage_two_border9.setOnClickListener(this);
//        colorImg = (ImageView)findViewById(R.id.colorImg);

        colorArray.add(roundImage_two_border1);
        colorArray.add(roundImage_two_border2);
        colorArray.add(roundImage_two_border3);
        colorArray.add(roundImage_two_border4);
        colorArray.add(roundImage_two_border5);
        colorArray.add(roundImage_two_border6);
        colorArray.add(roundImage_two_border7);
        colorArray.add(roundImage_two_border8);
        colorArray.add(roundImage_two_border9);

        for(RoundImageView r : colorArray){
            ColorDrawable c = (ColorDrawable)r.getDrawable();
            int _color = c.getColor();
            if(Color.red(_color)==Color.red(color)&&Color.green(_color)==Color.green(color)&&Color.blue(_color)==Color.blue(color)){
                r.setShowOutSide(true);
            }

        }

        back_icon = (ImageView)findViewById(R.id.back_icon);
        back_icon.setOnClickListener(this);
        complete = (Button)findViewById(R.id.complete);
        complete.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.back_icon:
                setResult(RESULT_CANCELED);
                finish();
                overridePendingTransition(R.anim.in_left, R.anim.out_right);
                break;
            case R.id.complete:
                Intent intent = new Intent();
                intent.putExtra("item", item);
                intent.putExtra("color", color);
                setResult(RESULT_OK, intent);
                finish();
                overridePendingTransition(R.anim.in_left, R.anim.out_right);
                break;
            case R.id.roundImage_two_border1:
            case R.id.roundImage_two_border2:
            case R.id.roundImage_two_border3:
            case R.id.roundImage_two_border4:
            case R.id.roundImage_two_border5:
            case R.id.roundImage_two_border6:
            case R.id.roundImage_two_border7:
            case R.id.roundImage_two_border8:
            case R.id.roundImage_two_border9:
                for(RoundImageView roundImageView : colorArray){
                    roundImageView.setShowOutSide(false);
                    if(roundImageView == v){
                        roundImageView.setShowOutSide(true);
                         color = ((ColorDrawable)roundImageView.getDrawable()).getColor();
//                        ColorDrawable d = new ColorDrawable(color);

                    }
                    roundImageView.invalidate();
                }
                break;
            default:
                break;
        }
    }


    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.in_left, R.anim.out_right);
    }
}
